<?php
if(!defined('BASEPATH'))
	exit('No direct script access allowed');

class M_user extends CI_Model {

	function __construct() {
		parent::__construct();
	}
	
	//get All Staff
	function getAllStaff()
	{
			$this->db->select('*,staff.name as staff_name,department.name as dep_name,
			staff.id as staff_id
			');
			$this->db->from('staff');
			
			$this->db->join('department', 'staff.department_id = department.id');
			
			$this->db->order_by("staff.id", "asc"); 
		
			$query = $this->db->get();
			
			if($query -> num_rows() > 0) {
				foreach($query->result() as $row) {
					$staff[] = $row;
				}
				
				return $staff;
		}
			
			
	}
	
	function getAllStaffAcess()
	{
		$this->db->select('*,staff.name as staff_name,role.name as role_name,login.id as login_login_id');
			$this->db->from('login');
			$this->db->join('staff', 'login.id = staff.login_id');
			$this->db->join('role', 'login.role_id = role.id');	
			$this->db->order_by("staff.id", "asc"); 
		
			$query = $this->db->get();
			
			if($query -> num_rows() > 0) {
				foreach($query->result() as $row) {
					$staff[] = $row;
				}
				
				return $staff;
		}
	}
	
	//get all staff who dont have an access
	function getAllStaffHasNoAcess()
	{
		$this->db->select('*,staff.name as staff_name,department.name as dep_name,
			staff.id as staff_id
			');
			$this->db->from('staff');
			
			$this->db->join('department', 'staff.department_id = department.id');
			
			$this->db->order_by("staff.id", "asc"); 
			$this->db->where('staff.login_id',NULL);
		
			$query = $this->db->get();
			
			if($query -> num_rows() > 0) {
				foreach($query->result() as $row) {
					$staff[] = $row;
				}
				
				return $staff;
		}
	}
	
	
	
	
	//get All Role
	function getAllRole() {
		$query = $this -> db -> get('role');

		if($query -> num_rows() > 0) {
			foreach($query->result() as $row) {
				$roles[] = $row;
			}

			return $roles;
		}

	}
	
	//get All Department
	function getAllDepartment() {
		$query = $this -> db -> get('department');

		if($query -> num_rows() > 0) {
			foreach($query->result() as $row) {
				$departments[] = $row;
			}

			return $departments;
		}

	}
	
	//add staff
	function addStaff($phone,$department,$id_card,$name,$gender,$address,$email,$dob)
	{
		 /*$this->db->trans_start();	
 
			//save to login table
			$array_login = array('username'=>$username,'password'=>$password,'role_id' => $role);

			//if staff
			if($role==$this->config->item('staff_role_id'))
			{
				//save to staff table
				$array_staff = array('id_card'=>$id_card,'name'=>$name,'gender' => $gender,
				'dob'=>$dob,
				'address'=>$address,
				'contact'=>$phone,
				'email'=>$email,
				'department_id'=>$department,
				'login_id'=>$latest_login_id
				);
				
			}
			else
			{
				$array_staff = array('id_card'=>$id_card,'name'=>$name,'gender' => $gender,
				'dob'=>$dob,
				'address'=>$address,
				'contact'=>$phone,
				'email'=>$email,
				'department_id'=>$departmentit,
				'login_id'=>$latest_login_id
				);
								
			}
			
			$addlogin=$this->db->insert('login',$array_login);
			$addstaff=$this->db->insert('staff',$array_staff);	
			

		if($addlogin && $addstaff)
		{
			return true;
		}
  		else
		{
			return false;
		}
			
                $this->db->trans_complete();
		
	}*/
	
	$array_staff = array('id_card'=>$id_card,'name'=>$name,'gender' => $gender,
				'dob'=>$dob,
				'address'=>$address,
				'contact'=>$phone,
				'email'=>$email,
				'department_id'=>$department,
				);
	if($this->db->insert('staff',$array_staff))
	{
		return true;	
	}	
	else
	{
		return false;
	}
	
	}

	function updateStaffLoginId($staff_id,$login_id)
	{
		
	
				// update id on login table
					$array_staff = array('login_id'=>$login_id);
				$this->db->where('staff.id',$staff_id);
	
			if($this->db->update('staff',$array_staff))
			{
				return true;
			}
	  		else
			{
				return false;
			}
				
	  
			
	
	}

	function addStaffAccess($username,$password,$role)
	{
			//save to login table
					$array_login = array('username'=>$username,'password'=>$password,'role_id'=>$role);
					
					if($this->db->insert('login',$array_login))
					{
						return true;
					}
					else
						{
							return false;
						}
	}

	function getStaffByName($staff_id)
	{
		$this->db->select('*,staff.name as staff_name,role.name as role_name,login.id as login_login_id');
			$this->db->from('login');
			$this->db->join('staff', 'login.id = staff.login_id');
			$this->db->join('role', 'login.role_id = role.id');	
			$this->db->order_by("staff.id", "asc"); 
			$this->db->where("staff.id", $staff_id); 
			
			$query = $this->db->get();
			
			if($query -> num_rows() > 0) {
				foreach($query->result() as $row) {
					$staff[] = $row;
				}
				
				return $staff;
		}
	}

	function getStaffLoginByName($login_id)
	{
		$this->db->select('*');
			$this->db->from('login');
			$this->db->where("login.id", $login_id); 
			
			$query = $this->db->get();
			
			if($query -> num_rows() > 0) {
				foreach($query->result() as $row) {
					$staff[] = $row;
				}
				
				return $staff;
		}
	}

	function editStaff($staff_id,$phone,$department,$id_card,$name,$gender,$address,$email,$dob)
	{
		$array_staff = array('id_card'=>$id_card,'name'=>$name,'gender' => $gender,
				'dob'=>$dob,
				'address'=>$address,
				'contact'=>$phone,
				'email'=>$email,
				'department_id'=>$department,
				);
				$this->db->where('staff.id',$staff_id);
	if($this->db->update('staff',$array_staff))
	{
		return true;	
	}	
	else
	{
		return false;
	}
	
	}
	
	function editStaffAccess($login_id,$username,$password,$role_id){
				if($password==null OR $password=='')
				{
					$arrah_login = array('username'=>$username,'role_id'=>$role_id
				);
				}
				else
					{
						$arrah_login = array('username'=>$username,'password'=>$password,'role_id'=>$role_id
				);
					}
			
				$this->db->where('login.id',$login_id);
	if($this->db->update('login',$arrah_login))
	{
		return true;	
	}	
	else
	{
		return false;
	}
	
	}
 
 	//delete staff 
	function deleteStaff($staff_id,$login_id)
	{
	
		if(!empty($login_id) || $login_id!=null)
		{
			//delete the login and delete staff
			
			$this->db->where('login.id', $login_id);
			$deleteLogin=$this->db->delete('login');
			
			$this->db->where('id', $staff_id);
			$deleteStaff=$this->db->delete('staff');
			
			if($deleteStaff && $deleteLogin)
			{
				return true;
			} 
			else
				{
					return false;
				}
		}

	}

	function deleteLoginStaff($login_id)
	{
			
			$this->db->where('login.id', $login_id);
			$deleteLogin=$this->db->delete('login');
			
			if($deleteLogin)
			{
				return true;
			} 
			else
				{
					return false;
				}
	}

}



